package dataexplorer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

class OpenFile extends AbstractAction{
    Algorithm algorithm;

    public OpenFile(Algorithm dataExplorerAlgorithm) {
        this.algorithm = dataExplorerAlgorithm;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser jFileChooser = new JFileChooser();
        int returnValue = jFileChooser.showOpenDialog(null);
        
        if(returnValue == JFileChooser.APPROVE_OPTION){
            String filename = jFileChooser.getSelectedFile().getAbsolutePath();
            
            System.out.println("WCZYTUJE PLIK "+filename);
            
            algorithm.loadData(filename);
        }        
    }
}

class GenerateVector extends AbstractAction{
    Algorithm algorithm;

    public GenerateVector(Algorithm dataExplorerAlgorithm) {
        this.algorithm = dataExplorerAlgorithm;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(algorithm.getData()==null){
            JOptionPane.showMessageDialog(null, "Wczytaj najpierw dane.", "Błąd", JOptionPane.ERROR_MESSAGE);
            return;
        }
        algorithm.generateVector();
    }
}

class ShowAnimation extends AbstractAction{
    Algorithm algorithm;

    public ShowAnimation(Algorithm dataExplorerAlgorithm) {
        this.algorithm = dataExplorerAlgorithm;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(algorithm.getData()==null){
            JOptionPane.showMessageDialog(null, "Wczytaj najpierw dane.", "Błąd", JOptionPane.ERROR_MESSAGE);
            return;
        }
        algorithm.generateVector();
        algorithm.setClicks(0);
        algorithm.showAnimation();
    }
}

class Classify extends AbstractAction{
    Algorithm algorithm;

    public Classify(Algorithm dataExplorerAlgorithm) {
        this.algorithm = dataExplorerAlgorithm;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(algorithm.getData()==null){
            JOptionPane.showMessageDialog(null, "Wczytaj najpierw dane.", "Błąd", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        int length = algorithm.getData().get(0).getAttributes().length;
        
        Double row[] = new Double[length];
        
        for(int i=0; i<length; i++){
            String p = JOptionPane.showInputDialog(null, "Podaj wartość atrybutu nr "+i, "Podaj wartość atrybutu nr "+i, JOptionPane.QUESTION_MESSAGE);
            try{
                row[i] = Double.parseDouble(p);
            } catch(Exception ex){
                JOptionPane.showMessageDialog(null, "Niepoprawne dane", "Błąd", JOptionPane.ERROR_MESSAGE);
                return;
            }
        }
        int clazz = algorithm.classify(row);

        JOptionPane.showMessageDialog(null, "Obiekt został sklasyfikowany do klasy "+clazz, "Wynik klasyfikacji", JOptionPane.INFORMATION_MESSAGE);
    }
}



public class GUI {

    private Algorithm Algorithm;
    private Animation animation;
    private JLabel statusBar;

    public JLabel getStatusBar() {
        return statusBar;
    }

    public void setStatusBar(JLabel statusBar) {
        this.statusBar = statusBar;
    }
    
    public GUI(){
        
        Algorithm Algorithm = new Algorithm();
        Animation animation = new Animation(Algorithm, this);
        
        JFrame jFrame = new JFrame();
        
        jFrame.setSize(600, 400);
        jFrame.setTitle("Eksploracja danych");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar jMenuBar = new JMenuBar();
        
        JMenu options = new JMenu("Opcje");
        JMenuItem jMenuItem;
        jMenuItem = new JMenuItem("Wczytaj plik");
        jMenuItem.addActionListener(new OpenFile(Algorithm));
        options.add(jMenuItem);
        jMenuItem = new JMenuItem("Generuj wektor");
        jMenuItem.addActionListener(new GenerateVector(Algorithm));
        options.add(jMenuItem);
        jMenuItem = new JMenuItem("Krok po kroku");
        jMenuItem.addActionListener(new ShowAnimation(Algorithm));
        options.add(jMenuItem);
        jMenuItem = new JMenuItem("Klasyfikuj obiekt");
        jMenuItem.addActionListener(new Classify(Algorithm));
        options.add(jMenuItem);
        
        jMenuBar.add(options);
        jFrame.setJMenuBar(jMenuBar);
        
        statusBar = new JLabel("");
        statusBar.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
                
        jFrame.setLayout(new BorderLayout());
        jFrame.add(statusBar, BorderLayout.SOUTH);
        
        jFrame.add(animation);
        
        jFrame.setVisible(true);
    }

}
